#include <vector>
using namespace std;

void dfs(vector<vector<char>> &grid, unsigned int row, unsigned int col) {
  if (row < 0 || col < 0 || row >= grid.size() || col >= grid[0].size()) {
    return;
  }
  if (grid[row][col] == '1') {
    grid[row][col] = '0';
    dfs(grid, row - 1, col);
    dfs(grid, row, col - 1);
    dfs(grid, row + 1, col);
    dfs(grid, row, col + 1);
  }
}

int numIslands(vector<vector<char>> &grid) {
  if (grid.empty()) {
    return 0;
  }
  unsigned int count = 0;
  unsigned int rows = grid.size();
  unsigned int cols = grid[0].size();
  for (unsigned int row = 0; row < rows; row++) {
    for (unsigned int col = 0; col < cols; col++) {
      if (grid[row][col] == '1') {
        // 执行同化操作
        count++;
        dfs(grid, row, col);
      }
    }
  }
  return count;
}